'''

    View Factory Test


'''

from PyQt4 import QtGui, QtOpenGL, QtCore, Qt

_REGISTRY ={
    "Browser":      ("Main", "Files"),
    "Jobs":         ("Main", "Utils"),
    "Shell":        ("Main", "Utils"),
    "Properties":   ("Main", "Editors"),
}


def GetTypes():
    return _REGISTRY.keys()

def GetTypesByGroup():
    ret = {}
    for type, groups in _REGISTRY.iteritems():
        for group in groups:
            if group not in ret:
                ret[group] = []
            ret[group].append( type )
    return ret
    
class ViewCreateAction( QtGui.QAction ):
    def __init__( self, viewType, owner, setTypeFunc ):
        QtGui.QAction.__init__( self, viewType, owner )
        self.viewType = viewType
        self.setTypeFunc = setTypeFunc
        
    def trigger( self ):
        self.setTypeFunc( self.viewType )


def GetCreateMenu(owner, setTypeFunc ):
    menu = QtGui.QMenu(owner)
    for group, types in GetTypesByGroup().iteritems():
        grpMenu = menu.addMenu( group )
        for t in types:
            action = ViewCreateAction(t,owner,setTypeFunc)
            grpMenu.addAction( action )
            owner.connect(
                action, QtCore.SIGNAL( "triggered()" ),
                action.trigger
            )
    return menu

def CreateView( type, parent ):
    w = None
    
    if type == 'Browser':
        w = QtGui.QLabel( type, parent )
    elif type == 'Jobs':
        w = QtGui.QLabel( type, parent )
    elif type == 'Properties':
        w = QtGui.QLabel( type, parent )
    elif type == 'Shell':
        w = QtGui.QWidget()
        lo = QtGui.QVBoxLayout()
        w.setStyleSheet( "* { background-color: #00FF00 } " )
        w.setMinimumSize( 100, 100 )
        container = QtGui.QX11EmbedContainer(w)
        lo.addWidget( container )
        w.setLayout( lo )
        wid = container.winId()
        parametersList = ["-into", str(wid), "-bg", "black", "-fg", "white"]
        process = QtCore.QProcess(container)
        process.start("xterm", parametersList)
        container.embedClient(0x340000e)
        w.connect( container, QtCore.SIGNAL("destroyed()"), process, QtCore.SLOT("kill()") )
    else:
        w = QtGui.QLabel( "???", parent )
    
    return w

